package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.CardSchemeDept;
import com.ruoyi.system.domain.SysRoleMenu;
import com.ruoyi.system.mapper.CardSchemeDeptMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CardSchemeMapper;
import com.ruoyi.system.domain.CardScheme;
import com.ruoyi.system.service.ICardSchemeService;

/**
 * 方案Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-25
 */
@Service
public class CardSchemeServiceImpl implements ICardSchemeService
{
    @Autowired
    private CardSchemeMapper cardSchemeMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private CardSchemeDeptMapper cardSchemeDeptMapper;

    /**
     * 查询方案
     *
     * @param schemeId 方案主键
     * @return 方案
     */
    @Override
    public CardScheme selectCardSchemeBySchemeId(Long schemeId)
    {
        return cardSchemeMapper.selectCardSchemeBySchemeId(schemeId);
    }

    /**
     * 查询方案列表
     *
     * @param cardScheme 方案
     * @return 方案
     */
    @Override
    public List<CardScheme> selectCardSchemeList(CardScheme cardScheme)
    {
        return cardSchemeMapper.selectCardSchemeList(cardScheme);
    }

    /**
     * 新增方案
     *
     * @param cardScheme 方案
     * @return 结果
     */
    @Override
    public int insertCardScheme(CardScheme cardScheme)
    {
        cardScheme.setCreateTime(DateUtils.getNowDate());
        cardScheme.setSchemePlan(cardScheme.getUserId() + String.valueOf(System.currentTimeMillis()));
        cardSchemeMapper.insertCardScheme(cardScheme);
        return insertSchemeMenu(cardScheme);
    }

    private int insertSchemeMenu(CardScheme cardScheme) {
        int rows = 1;
        // 新增方案与部门管理
        List<CardSchemeDept> list = new ArrayList<>();
        for (Long deptId : cardScheme.getDeptIds())
        {
            CardSchemeDept csd = new CardSchemeDept();
            csd.setSchemePlan(cardScheme.getSchemePlan());
            csd.setDeptId(deptId);
            SysDept sysDept = deptMapper.selectDeptById(deptId);
            csd.setDeptName(sysDept.getDeptName());
            list.add(csd);
        }
        if (list.size() > 0)
        {
            rows = cardSchemeDeptMapper.batchSchemeDeptMenu(list);
        }
        return rows;
    }

    /**
     * 修改方案
     *
     * @param cardScheme 方案
     * @return 结果
     */
    @Override
    public int updateCardScheme(CardScheme cardScheme)
    {
        cardScheme.setUpdateTime(DateUtils.getNowDate());
        cardSchemeMapper.updateCardScheme(cardScheme);
        cardSchemeDeptMapper.deleteCardSchemeDeptBySchemePlan(cardScheme.getSchemePlan());
        return insertSchemeMenu(cardScheme);
    }

    /**
     * 批量删除方案
     *
     * @param schemeIds 需要删除的方案主键
     * @return 结果
     */
    @Override
    public int deleteCardSchemeBySchemeIds(Long[] schemeIds)
    {
        // return cardSchemeMapper.deleteCardSchemeBySchemeIds(schemeIds);
        return cardSchemeMapper.updateCardSchemeBySchemeIds(schemeIds);
    }

    /**
     * 删除方案信息
     *
     * @param schemeId 方案主键
     * @return 结果
     */
    @Override
    public int deleteCardSchemeBySchemeId(Long schemeId)
    {
        return cardSchemeMapper.deleteCardSchemeBySchemeId(schemeId);
    }

    @Override
    public boolean checkSchemeNameUnique(CardScheme cardScheme) {
        Long schemeId = StringUtils.isNull(cardScheme.getSchemeId()) ? -1L : cardScheme.getSchemeId();
        CardScheme info = cardSchemeMapper.checkSchemeNameUnique(cardScheme);
        if (StringUtils.isNotNull(info) && info.getSchemeId().longValue() != schemeId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public List<Long> selectDeptListBySchemeId(Long schemeId) {
        return cardSchemeMapper.selectDeptListBySchemeId(schemeId);
    }

    @Override
    public List<CardSchemeDept> getSchemeDept(CardScheme cardScheme) {
        return cardSchemeMapper.getSchemeDept(cardScheme);
    }
}
